package com.southminority.ethnic.service.language.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southminority.ethnic.controller.language.vo.LanguagePageResq;
import com.southminority.ethnic.pojo.MinorityLanguageCulture;
import com.southminority.ethnic.pojo.MinorityLanguageSentence;
import com.southminority.ethnic.pojo.MinorityLanguageSpeech;
import com.southminority.ethnic.service.language.MinorityLanguageCultureService;
import com.southminority.ethnic.mapper.language.MinorityLanguageCultureMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
* @author ASHTR
* @description 针对表【minority_language_culture(语言数据库-口传文化)】的数据库操作Service实现
* @createDate 2025-08-29 00:33:49
*/
@Service
public class MinorityLanguageCultureServiceImpl extends ServiceImpl<MinorityLanguageCultureMapper, MinorityLanguageCulture>
    implements MinorityLanguageCultureService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean InputList(List<MinorityLanguageCulture> list) {
        int batchSize = 500;  // 每批500条
        for (int i = 0; i < list.size(); i += batchSize) {
            List<MinorityLanguageCulture> subList = list.subList(i, Math.min(i + batchSize, list.size()));
            try {
                this.saveOrUpdateBatch(subList);
            } catch (Exception e) {
                log.error("批量插入口传文化数据失败，批次: "+i+" - "+(i + batchSize)+"\n失败原因："+e.getMessage());
                throw new RuntimeException(
                        "批量插入口传文化数据失败，批次: " + i + " - " + (i + batchSize)+ "\n失败原因："+e.getMessage()
                );
            }

        }
        return true;
    }

    @Override
    public LanguagePageResq<MinorityLanguageCulture> selectPageKey(Integer pageNum, Integer pageSize, String key, List<Integer> minorityIds) {
        Page<MinorityLanguageCulture> page=new Page(pageNum,pageSize);
        baseMapper.selectSpeechWithTagCmPage(page,key,minorityIds);
        LanguagePageResq<MinorityLanguageCulture> mypage=new LanguagePageResq<>();
        mypage.getPage(page);
        //获取内容

        List<MinorityLanguageCulture> list=new ArrayList<>();
        if (page.getRecords() != null && !page.getRecords().isEmpty()) {
            list=baseMapper.selectSpeechWithTagSonFromList(page.getRecords(),minorityIds);
        }
        //存储
        mypage.setList(list);
        return mypage;
    }

    @Override
    public Page<MinorityLanguageCulture> selectOnesPageKey(Integer pageNum, Integer pageSize, String key, List<Integer> minorityIds) {
        Page<MinorityLanguageCulture> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<MinorityLanguageCulture> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(MinorityLanguageCulture::getTheme,
                MinorityLanguageCulture::getChineseMean,
                MinorityLanguageCulture::getId);
        if (key!=null&& !key.isEmpty()){
            wrapper.like(MinorityLanguageCulture::getChineseMean,key)
                    .or()
                    .like(MinorityLanguageCulture::getTheme,key);
        }
        if (minorityIds != null && !minorityIds.isEmpty()){
            wrapper.in(MinorityLanguageCulture::getMinorityCategory,minorityIds);
        }
        wrapper.orderByAsc(MinorityLanguageCulture::getId);
        return baseMapper.selectPage(page,wrapper);
    }

    @Override
    public synchronized Boolean add(MinorityLanguageCulture minorityLanguageCulture) {
        LambdaQueryWrapper<MinorityLanguageCulture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MinorityLanguageCulture::getChineseMean,minorityLanguageCulture.getChineseMean())
                .eq(MinorityLanguageCulture::getMinorityCategory,minorityLanguageCulture.getMinorityCategory());
        if (!baseMapper.selectObjs(wrapper).isEmpty()){
            throw  new RuntimeException("已经存在该数据,请勿重复添加");
        }
        return baseMapper.insert(minorityLanguageCulture)>0;
    }

    @Override
    @Transactional
    public Boolean delete(Integer id) {
        //设置获取当前时间yyyyMMddHHmmss
        String time= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        MinorityLanguageCulture minorityLanguageCulture=baseMapper.selectByIdForUpdate(id);
        if (minorityLanguageCulture==null){
            throw new RuntimeException("该数据不存在,请检查ID");
        }else{
            minorityLanguageCulture.setTheme(minorityLanguageCulture.getTheme()+"(已删除-"+time+")");
            baseMapper.updateById(minorityLanguageCulture);
            return baseMapper.deleteById(id)>0;
        }
    }

    @Override
    public synchronized Boolean updata(MinorityLanguageCulture minorityLanguageCulture) {
        return baseMapper.updateById(minorityLanguageCulture)>0;
    }
}




